﻿package com.rbac.system.controller;

import com.rbac.system.common.RequirePermission;
import com.rbac.system.common.Result;
import com.rbac.system.dto.PermissionDTO;
import com.rbac.system.service.PermissionService;
import com.rbac.system.vo.PermissionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/permission")
@CrossOrigin
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 新增权限
     */
    @PostMapping("/add")
    @RequirePermission("admin:permission:add")
    public Result<String> addPermission(@RequestBody PermissionDTO permissionDTO) {
        return permissionService.addPermission(permissionDTO);
    }

    /**
     * 修改权限
     */
    @PutMapping("/update/{id}")
    @RequirePermission("admin:permission:update")
    public Result<String> updatePermission(@PathVariable Long id, @RequestBody PermissionDTO permissionDTO) {
        return permissionService.updatePermission(id, permissionDTO);
    }

    /**
     * 删除权限
     */
    @DeleteMapping("/delete/{id}")
    @RequirePermission("admin:permission:delete")
    public Result<String> deletePermission(@PathVariable Long id) {
        return permissionService.deletePermission(id);
    }

    /**
     * 根据ID查询权限详情
     */
    @GetMapping("/{id}")
    @RequirePermission("admin:permission:view")
    public Result<PermissionVO> getPermissionById(@PathVariable Long id) {
        return permissionService.getPermissionById(id);
    }

    /**
     * 查询所有权限（树形结构）
     */
    @GetMapping("/list")
    @RequirePermission("admin:permission:list")
    public Result<List<PermissionVO>> getAllPermissions() {
        return permissionService.getAllPermissions();
    }

    /**
     * 根据权限类型查询权限
     */
    @GetMapping("/type/{permissionType}")
    @RequirePermission("admin:permission:list")
    public Result<List<PermissionVO>> getPermissionsByType(@PathVariable String permissionType) {
        return permissionService.getPermissionsByType(permissionType);
    }

    /**
     * 查询启用的权限
     */
    @GetMapping("/enabled")
    @RequirePermission("admin:permission:list")
    public Result<List<PermissionVO>> getEnabledPermissions() {
        return permissionService.getEnabledPermissions();
    }

    /**
     * 根据父级ID查询子权限
     */
    @GetMapping("/children/{parentId}")
    @RequirePermission("admin:permission:list")
    public Result<List<PermissionVO>> getPermissionsByParentId(@PathVariable Long parentId) {
        return permissionService.getPermissionsByParentId(parentId);
    }

    /**
     * 根据用户ID获取用户权限
     */
    @GetMapping("/user/{userId}")
    @RequirePermission("admin:user:permission:view")
    public Result<List<String>> getUserPermissions(@PathVariable Long userId) {
        List<String> permissions = permissionService.getUserPermissions(userId);
        return Result.success(permissions);
    }
} 

